<?php

namespace app\common\traits;

use app\common\model\Company;
use app\common\service\annotation\NodeAnnotation;
use app\common\service\MenuService;
use app\company\model\CompanyUser;
use think\facade\Cache;
use think\response\Json;

trait InitTrait
{
    /**
     * @NodeAnnotation(title="初始化接口")
     */
    public function index(): Json
    {
//        $cacheData = Cache::get("init_{$this->appName}_" . $this->uid);
//        if (!empty($cacheData)) {
//            return $this->success($cacheData);
//        }
        $appId = $this->appName == 'admin' ? $this->groupId : $this->companyId;
        $menuService = new MenuService($this->uid, $this->appName, $appId);
        $data        = [
                'logoInfo' => [
                    //                        'title' => sysConfig('site', 'logo_title'),
                    //                        'image' => sysConfig('site', 'logo_image'),
                ],
                //                            'homeInfo' => $menuService->getHomeInfo(),
                'menuInfo' => $menuService->getMenuTree(),
//                'userInfo' => $this->userInfo,
                'userInfo' => (new CompanyUser())->with([
                    'userAuth' => function ($query) {
                        $query->with(['auth']);
                    }
                ])->find($this->uid),
                'center_local' => $this->position,
        ];
        if($this->appName == 'company'){
            $data['companyInfo'] =  (new Company())->find($this->companyId);
        }
//        Cache::tag("init_{$this->appName}_" . $this->uid)->set("init_{$this->appName}_" . $this->uid, $data);
        return $this->success($data);
    }

    /**
     * @NodeAnnotation(title="清理缓存接口")
     */
    public function clearCache(): Json
    {
        // 清除特定标签的所有缓存
        Cache::delete("init_{$this->appName}_" . $this->uid);
        return $this->success('清理缓存成功');
    }
}